################################################################################
# Libraries and Import: All respondents
################################################################################
library(cjoint)
library(gridExtra)
library(grid)
library(ggplotify)

load(file="data/dat_analysis_CN.RData")

################################################################################
# Assign attributes and levels
################################################################################
# Attribute list
names(dat_conjoint)[5:11] <- c("Yasukuni Shrine", "OBOR Cooperation", 
                                "China Tariff", "Japan Tariff",
                                "Japan Constitution", 
                                "Diaoyu Senkaku Sovereignty",
                                "Diaoyu Senkaku Dev")

# Yasukuni
attribute_list <- list()
attribute_list[["Yasukuni Shrine"]] <- c("Resume visits", 
                                         "Suspend visits (informal)", 
                                         "Suspend visits (formal)")
dat_conjoint$`Yasukuni Shrine` <- factor(dat_conjoint$`Yasukuni Shrine`,
       levels = c("日本首相继续参拜靖国神社", 
                  "日本首相暂停参拜靖国神社，但不正式承诺停止参拜靖国神社",
                  "日本首相做出正式承诺，未来不再参拜靖国神社"), 
       labels = c("Resume visits",
                  "Suspend visits (informal)",
                  "Suspend visits (formal)"))

# OBOR
attribute_list[["OBOR Cooperation"]] <- c("Non-cooperation", "Cooperation")
dat_conjoint$`OBOR Cooperation` <- factor(dat_conjoint$`OBOR Cooperation`,
                             levels = c("日本推行独立于中国的“自由、开放的印度-太平洋”战略，与中国的“一带一路”政策竞争",
                                        "日本修改“自由、开放的印度-太平洋”战略并与中国的“一带一路”政策相结合，协调与中国的区域合作"), 
                             labels = c("Non-cooperation", "Cooperation"))

# China Tariff
attribute_list[["China Tariff"]] <- c("No change", 
                                      "Increase 10%", "Increase 20%", 
                                      "Decrease 10%", "Decrease 20%")
dat_conjoint$`China Tariff` <- factor(dat_conjoint$`China Tariff`,
                             levels = c("中国不改变对日本产品税率",
                                        "中国对日本产品税率增高10%", 
                                        "中国对日本产品税率增高20%",
                                        "中国对日本产品税率降低10%",
                                        "中国对日本产品税率降低20%"), 
                             labels = c("No change", 
                                        "Increase 10%", "Increase 20%", 
                                        "Decrease 10%", "Decrease 20%"))
# Japan Tariff
attribute_list[["Japan Tariff"]] <- c("No change", 
                                      "Increase 10%", "Increase 20%", 
                                      "Decrease 10%", "Decrease 20%")
dat_conjoint$`Japan Tariff` <- factor(dat_conjoint$`Japan Tariff`,
                             levels = c("日本不改变对中国产品税率",
                                        "日本对中国产品税率增高10%", 
                                        "日本对中国产品税率增高20%",
                                        "日本对中国产品税率降低10%",
                                        "日本对中国产品税率降低20%"), 
                             labels = c("No change", 
                                        "Increase 10%", "Increase 20%", 
                                        "Decrease 10%", "Decrease 20%"))

attribute_list[["Japan Constitution"]] <- c("No amendment", "Amendment")
dat_conjoint$`Japan Constitution` <- factor(dat_conjoint$`Japan Constitution`,
                             levels = c("日本不修改宪法以赋予自卫队正式存在权利", 
                                        "日本修改宪法并赋予自卫队正式存在权利"), 
                             labels = c("No amendment", "Amendment"))

# Diayu/Senkaku Development
attribute_list[["Diaoyu Senkaku Dev"]] <- c("Status quo", "Joint development", 
                                            "China development", "Japan development")
dat_conjoint$`Diaoyu Senkaku Dev` <- factor(dat_conjoint$`Diaoyu Senkaku Dev`,
                             levels = c("中国和日本共同维持现状，不开发钓鱼岛及周边海域的资源", 
                                        "中国和日本共同开发并且平等享有钓鱼岛及周边海域的资源", 
                                        "由中国单方面开发钓鱼岛及周边海域的资源", 
                                        "由日本单方面开发钓鱼岛及周边海域的资源"), 
                             labels = c("Status quo", "Joint development", 
                                        "China development", "Japan development"))

# Diayu/Senkaku Sovereignty
attribute_list[["Diaoyu Senkaku Sovereignty"]] <- c("Status quo",
                                                    "Joint sovereignty", 
                                                    "China sovereignty",
                                                    "Japan sovereignty")
dat_conjoint$`Diaoyu Senkaku Sovereignty` <- factor(dat_conjoint$`Diaoyu Senkaku Sovereignty`,
                                    levels = c("中国和日本共同维持现状，双方均不享有钓鱼岛主权",
                                               "中国和日本共享钓鱼岛主权",
                                               "双方承认，中国单方面享有钓鱼岛主权",
                                               "双方承认，日本单方面享有钓鱼岛主权"), 
                                    labels = c("Status quo", 
                                               "Joint sovereignty", 
                                               "China sovereignty", 
                                               "Japan sovereignty"))

################################################################################
# Define conflicting levels
################################################################################
constraint_list <- list()

constraint_list[[1]] <- list()
constraint_list[[1]][["Diaoyu Senkaku Sovereignty"]] <- c("Joint sovereignty")
constraint_list[[1]][["Diaoyu Senkaku Dev"]] <- c("Status quo")


constraint_list[[2]] <- list()
constraint_list[[2]][["Diaoyu Senkaku Sovereignty"]] <- c("Japan sovereignty")
constraint_list[[2]][["Diaoyu Senkaku Dev"]] <- c("Joint development")

################################################################################
# Analysis
################################################################################
# Declare design
China_japandesign <- makeDesign(type='constraints', attribute.levels=attribute_list,
                                constraints=constraint_list)

# Run AMCE and print summary
summary(amce(formula = chosen ~ `China Tariff` + `Japan Constitution` + `Japan Tariff` + 
               + `OBOR Cooperation` + `Diaoyu Senkaku Sovereignty` + `Diaoyu Senkaku Dev` + `Yasukuni Shrine`,
             data = dat_conjoint, design = China_japandesign, cluster=TRUE, respondent.id = "id"))

# Run AMCE regression and store results
amce_all <- amce(formula = chosen ~ `China Tariff` + `Japan Constitution` + `Japan Tariff` + 
                  + `OBOR Cooperation` + `Diaoyu Senkaku Sovereignty` + `Diaoyu Senkaku Dev` + `Yasukuni Shrine`, 
        data = dat_conjoint, design = China_japandesign, cluster=TRUE, respondent.id = "id")

# Estimate model using OLS
ols_all <- lm(chosen ~ `China Tariff` + `Japan Constitution` + 
                       `Japan Tariff` + `OBOR Cooperation` + `Diaoyu Senkaku Sovereignty` + 
                       `Diaoyu Senkaku Dev` + `Yasukuni Shrine`, 
                        data = dat_conjoint)

################################################################################
# Plot
################################################################################
# Create conjoint plot
plot(amce_all,  
     group.order = c("OBOR Cooperation", "China Tariff", "Japan Tariff", 
                     "Yasukuni Shrine", "Japan Constitution", 
                     "Diaoyu Senkaku Sovereignty", "Diaoyu Senkaku Dev"),
     xlab = "Change in prob. of choosing",
     ggtitle("China"),
     size = 0.2,
     xlim = c(-0.25, 0.25),
     colors = c("grey60", "black", "grey60", "black", "black", "black", "grey60"),
     plot.theme = theme_bw() + 
     theme(panel.grid.major = element_line(colour = "grey98")) + 
     theme(legend.position="none") +
     theme(axis.ticks = element_blank()) + 
     theme(axis.title.x = element_text(size = 10)) +
     theme(plot.title = element_text(size=12)) +
     theme(plot.title = element_text(hjust = 0.5)) +
     theme(axis.text.y = element_text(hjust=0)),
     text.color = "black",
     text.size = 14
)

# Save conjoint plot
amce_china_chart = last_plot()
save(amce_china_chart, file="data/charts.RData")

# Export to Stata for AME analysis
write.csv(dat_conjoint, file = "data/conjoint_cn.csv", row.names=FALSE)